package com.atguigu.gmall.product.comtroller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 * sku信息管理 controller 组件
 * @author : zjj
 * @date : 2021/1/29 17:17
 */
@RestController
@Api(tags = "sku信息管理接口")
@RequestMapping("admin/product")
public class SkuManagerController {


    /**
     * 商品基本信息管理mapper组件
     */
    @Autowired
    private ManagerService managerService;

    /**
     * 根据spuId 获取到所有的spu图片
     * @param spuId spuId
     * @return      spu图片列表
     */
    @GetMapping("spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        return Result.ok(managerService.getSpuImageList(spuId));
    }

    /**
     * 根据spuId 获取销售属性
     * @param spuId spuId
     * @return      销售属性
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable("spuId")Long spuId){
        return Result.ok(managerService.getSpuSaleAttrList(spuId));

    }

    /**
     * 保存商品sku信息
     * @param skuInfo   sku信息
     * @return          处理结果
     */
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }


    /**
     * 分页查询sku信息列表
     * @param page      当前页
     * @param limit     每页大小
     * @return          分页数据
     */
    @GetMapping("list/{page}/{limit}")
    public Result<IPage<SkuInfo>> getSkuInfoPage(
            @ApiParam(name = "page",value = "当前页",required = true) @PathVariable Long page,
            @ApiParam(name = "limit",value = "每页显示的条数",required = true) @PathVariable Long limit

    ){
        Page<SkuInfo> skuInfoPage  = new Page<>(page,limit);
        IPage<SkuInfo> skuInfos = managerService.selectPage(skuInfoPage);
        return Result.ok(skuInfos);
    }

    /**
     * 商品上架操作
     * @param skuId 商品skuId
     * @return      处理结果
     */
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable("skuId")Long skuId){
        managerService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 商品下架操作
     * @param skuId 商品skuId
     * @return      处理结果
     */
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId")Long skuId){
        managerService.cancelSale(skuId);
        return Result.ok();
    }





}
